package edu.chinasoft.servlet;

import edu.chinasoft.domain.User;
import edu.chinasoft.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;*/

/**
 * 
 * @author 
 * @date 2020年2月14日:下午5:42:58
 * @version V1.0
 * @Description: 
 * 
*/
@WebServlet(name="myUpdateUserStatusServlet",value="/servlet/updateuserstatusservlet")
public class  UpdateUserStatusServlet  extends HttpServlet {
	private static Logger logger = LoggerFactory.getLogger(UpdateUserStatusServlet.class);
	private UserService userService;
	/**
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		logger.debug(" UpdateUserStatusServlet >doGet start.......");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
		UserService userServiceImp = wac.getBean("userService", UserService.class);
		this.userService = userServiceImp;
		String user_id = request.getParameter("id");
		String user_status = request.getParameter("user_status");
		user_status = (user_status.equals("1"))?"0":"1";
		User user = new User();
		user.setUserId(Integer.parseInt(user_id));
		user.setUserStatus(user_status);
		boolean result = this.userService.updateUserStatusById(user);
		
		if(result) {//更新成功
			response.sendRedirect(""+request.getContextPath()+"/servlet/showuserservlet");
		}else {//更新失败
			response.sendRedirect(""+request.getContextPath()+"/updateuserstatuserror.jsp");
		}
			
		
		logger.debug(" UpdateUserStatusServlet >doGet end.......");
	}
	
	/**
	 * 
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		logger.debug(" UpdateUserStatusServlet >doPost start.......");
		this.doGet(request, response);
		
		logger.debug(" UpdateUserStatusServlet >doPost end.......");
	}
	
}
